package org.bspv.pharma.dao.impl;

import java.util.Date;
import java.util.List;

import org.bspv.pharma.dao.StandardEquipmentDetailDAO;
import org.bspv.pharma.model.Place;
import org.bspv.pharma.model.StandardEquipmentDetail;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author guillaume
 * 
 */
@Repository(value = "standardEquipmentDetailDAO")
public class StandardEquipmentDetailDAOImpl extends
		AbstractHibernateDAO<StandardEquipmentDetail, Long> implements
		StandardEquipmentDetailDAO {

	@Autowired
	public StandardEquipmentDetailDAOImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	public List<StandardEquipmentDetail> findAllStandardEquipmentDetails(
			Place place) {
		Date today = new Date();
		DetachedCriteria crit = DetachedCriteria.forClass(getManagedType())
				.createCriteria("standardEquipment")
				.add(Restrictions.le("beginningOfValidity", today))
				.add(Restrictions.ge("endOfValidity", today))
				.createCriteria("place")
				.add(Restrictions.ge("lowerMarker", place.getLowerMarkup()))
				.add(Restrictions.le("upperMarker", place.getUpperMarkup()));

		List<StandardEquipmentDetail> lst = getHibernateTemplate()
				.findByCriteria(crit);
		return lst;
	}
}
